<?php

namespace app\admin\controller\sms;
use app\admin\model\sms\SmsserviceModel;
use app\admin\model\sms\TemplateModel;
use app\common\annotation\NodeAnnotation;


use app\common\controller\Admin;
use app\common\traits\ControllerCurd;
use SmsAliba\Sms;

/**
 * Class Administrator
 * @package app\admin\controller
 * @NodeAnnotation(
 *     title="模板",
 *     parent="default",
 *     is_menu=0,
 *     is_view=0,
 *     auth=1,
 * )
 */
class Template extends Admin
{
    use ControllerCurd;

    public $model;

    public function initialize()
    {
        parent::initialize();
        $this->model = new TemplateModel();
    }

    /**
     * @NodeAnnotation(
     *     title="模板列表",
     *     parent="admin/sms.template",
     *     is_menu=1,
     *     is_view=1,
     *     auth=1,
     *     MenuRoot="admin/sms.smsservice"
     * )
     */
    public function index()
    {
        $keywords = $this->request->param("keywords");
        $service_id = $this->request->param("service_id",0,'intval');

        $where  = [];

        if($keywords) array_push($where,["template_name","eq",$keywords]);
        if($service_id) array_push($where,["service_id","eq",$service_id]);

        $data = $this->model->selectPageDatas(1,20,$where);
        return view('',[
            "dataList" => $data,
            "search"   => [
                "keywords" => $keywords,
            ]
        ]);
    }


    /**
     * @NodeAnnotation(
     *     title="创建短信模板",
     *     parent="admin/sms.template",
     *     is_menu=0,
     *     is_view=1,
     *     auth=1,
     *     MenuRoot="admin/sms.smsservice"
     * )
     */
    public function addtemplate()
    {
        $serviceList = (new SmsserviceModel())->select();
        $service_id = $this->request->param("service_id",0,'intval');
        $this->add(url('admin/sms.template/index'));
        return view('',[
            'service_id' => $service_id,
            'serviceList'=> $serviceList
        ]);
    }

    /**
     * @NodeAnnotation(
     *     title="编辑短信模板",
     *     parent="admin/sms.template",
     *     is_menu=0,
     *     is_view=1,
     *     auth=1,
     *     MenuRoot="admin/sms.smsservice"
     * )
     */
    public function edittemplate()
    {
        $id = $this->request->param("id",0,'intval');
        $data = $this->model->where('id',$id)->find();
        $serviceList = (new SmsserviceModel())->select();
        $this->edit(url('admin/sms.template/index'));
        return view('',[
            'data' => $data,
            'serviceList'=> $serviceList
        ]);
    }

    /**
     * @NodeAnnotation(
     *     title="删除短信服务",
     *     parent="admin/sms.template",
     *     is_menu=0,
     *     is_view=1,
     *     auth=1,
     *     MenuRoot="admin/sms.smsservice"
     * )
     */
    public function deltemplate()
    {
        $this->del(url('admin/sms.template/index'));
    }

    /**
     * @NodeAnnotation(
     *     title="短信测试",
     *     parent="admin/sms.template",
     *     is_menu=0,
     *     is_view=1,
     *     auth=1,
     *     MenuRoot="admin/sms.smsservice"
     * )
     */
    public function test()
    {
        $id = $this->request->param("id",0,'intval');
        if($this->request->isPost())
        {
            $template = $this->model->where('id',$id)->find();
            if($template)
            {
                $telphone = $this->request->param("telphone");
                $service = (new SmsserviceModel())->where('id',$template['service_id'])->find();
                $contents = $this->request->param('content');
                if(!$telphone) $this->error("请输入手机号");
                if(!$service) $this->error("请选择服务平台");

                $content = '';
                switch ($template['template_type'])
                {
                    case 1:
                        $content = $contents;
                        break;
                    case 2:
                        $content = mt_rand(1111,9999);
                        break;
                    case 3:
                        $content = $contents;
                        break;
                }

                $sms = new Sms();
                $sms->setAccessKeyId($service['value']['accessKeyId']);
                $sms->setAccessKeySecret($service['value']['accessKeySecret']);
                $sms->setSignName($template['template_sign']);
                $sms->setTemplateCode($template['template_code']);
                $sms->setTemplateContent($content,$template['template_type']);
                $result = $sms->sendSms($telphone);

                if(isset($result['data']['Code']) && $result['data']['Code']=='OK')
                {
                    $this->success("发送成功");
                }
                $this->error("发送失败");
            }
            $this->error("测试失败");
        }

        return view('',[
            "template_id" => $id
        ]);
    }
}